/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron.Sprites;

import YottaTron.*;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Prekazky, ktere brani robotum v ceste
 * @author Cenda
 */
public class ObstacleSprite extends Sprite implements Serializable{
    private Runner runner;
    int hitPoints;
    int maxHitPoints;
    
    public GraphicsLoader obstacleGraphic;
    public ImagesPlayer obstacleAnimation;
    public GraphicsLoader[] obstacleGraphics;
    public ImagesPlayer[] obstacleAnimations;
    public ImagesPlayer burningAnimation;
    public boolean damageable;
    public boolean destructible;
    public boolean ignitable;
    public boolean burning;

    public ObstacleSprite(double x, double y,  int ID, GraphicsLoader[] obstacleGraphics, Runner r) {
        super(x, y, obstacleGraphics[0].getImage(0).getWidth(), obstacleGraphics[0].getImage(0).getHeight(), ID);
        this.obstacleGraphic = obstacleGraphics[0];
        this.obstacleGraphics = obstacleGraphics;
        this.runner = r;
    }

    public ObstacleSprite(double x, double y,  int ID, ImagesPlayer[] obstacleAnimations, Runner r) {
        super(x, y, obstacleAnimations[0].getCurrentImage().getWidth(), obstacleAnimations[0].getCurrentImage().getHeight(), ID);
        this.obstacleAnimation = obstacleAnimations[0];
        this.obstacleAnimations = obstacleAnimations;
        this.runner = r;
    }

    public ObstacleSprite(double x, double y,  int ID, GraphicsLoader obstacleGraphics, Runner r) {
        super(x, y, obstacleGraphics.getImage(0).getWidth(), obstacleGraphics.getImage(0).getHeight(), ID);
        this.obstacleGraphic = obstacleGraphics;
        this.runner = r;
    }

    // konstruktoru pro pouziti v editoru
    public ObstacleSprite(double x, double y, int ID, GraphicsLoader obstacleGraphics) {
        super(x, y, obstacleGraphics.getImage(0).getWidth(), obstacleGraphics.getImage(0).getHeight(), ID);
        this.obstacleGraphic = obstacleGraphics;
    }


    @Override
    public ArrayList<Point2D> getShape() {
        if(damageable){
            return obstacleGraphics[0].getShape();
            //return obstacleGraphic.getShape();
        }else{
            return obstacleGraphic.getShape();
        }
    }

    public void setDamageable(boolean damageable) {
        this.damageable = damageable;
    }

    public void setDestructible(boolean destructible) {
        this.destructible = destructible;
    }

    public void setIgnitable(boolean ignitable) {
        this.ignitable = ignitable;
    }
    public void setBurningAnimation(ImagesPlayer animation){
    this.burningAnimation = animation;
    }

    public void setMaxHitPoints(int maxHitPoints) {
        this.maxHitPoints = maxHitPoints;
        hitPoints = maxHitPoints;
    }

    public void damage(int damage, boolean incendiary) {
        hitPoints -= damage;
        runner.map.getObstacleByID(ID).hitPoints = hitPoints;
        checkHitPoints();
        if(!burning&&hitPoints>0&&incendiary&&ignitable){
        runner.ShowAnimationAt(burningAnimation, (int)getX(), (int)getY());
        runner.burningObstacles.add(this);
        burning = true;
        }
       

    }

    @Override
    public void damage(int damage) {
        hitPoints -= damage;
        runner.map.getObstacleByID(ID).hitPoints = hitPoints;
        checkHitPoints();

    }

    public void checkHitPoints() {
        if (damageable) {
            if (hitPoints > 0.8*maxHitPoints) {
                obstacleGraphic = obstacleGraphics[1];
            } else if (hitPoints > 0.6*maxHitPoints) {
                obstacleGraphic = obstacleGraphics[2];
            } else if (hitPoints > 0.4*maxHitPoints) {
                obstacleGraphic = obstacleGraphics[3];
            } else {
                obstacleGraphic = obstacleGraphics[4];
            }
        }
        if (hitPoints < 0 && destructible) {
            destroySprite(runner);

        }


    }

    @Override
    public void destroySprite(Runner r) {
        r.disposeAnimation(burningAnimation);
        r.dePopulate(this);
        r.removeObstacle(this);
        r.burningObstacles.remove(this);
        System.out.println("prekazka znicena");
        //odstraneni z vygenerovane mapy aby se pozdeji prihlasenym hracum poslal aktualni stav
        r.map.removeObstacleByID(ID);
    }
     //zjisti na kterych polickach herniPole se sprite nachazi
    @Override
    public ArrayList<Point2D> getOccupiedCells(){
    ArrayList<Point2D> occupiedCells = new ArrayList<Point2D>();
        for (Iterator<Point2D> it = obstacleGraphic.getShape().iterator(); it.hasNext();) {
            Point2D point = it.next();
            occupiedCells.add(new Point(runner.XToTiles((int) (point.getX() + getX())),runner.YToTiles((int) (point.getY() + getY()))));
        }
    return occupiedCells;
    }
}
